/*
  Precios especiales por comprobante
  Arreglo de un trigger que habia quedado mal
*/


CREATE OR REPLACE FUNCTION tg_artm_ins_artmpre()
  RETURNS trigger AS
$BODY$
DECLARE
vfecha       varchar;
vtipoprepaga comm.tipoprepaga%TYPE;
vcodprepag   comm.codprepag%TYPE;
vtipopada    pad.tipopada%TYPE;
vcodpadr     pad.codpadr%TYPE;
vidpad       comm.idpad%TYPE;
vtipoarta    art.tipoarta%TYPE;
vcodartr     art.codartr%TYPE;
vcodambsuc   comm.codambsuc%TYPE;
vcodambtur   comm.codambtur%TYPE;
vnrocierre   comm.nrocierre%TYPE; 
vtipocom     comm.tipocom%TYPE;
/* En el alta de artm, guarda valores de precios historicos para cada varpre que
* tenga este stributo (pre.artmpre = 'S')
*/
BEGIN
SELECT CAST(comm.fecha AS VARCHAR), comm.tipoprepaga, comm.codprepag, 
pad.tipopada, pad.codpadr, comm.idpad,
NULL, NULL, 
comm.codambsuc, comm.codambtur, comm.nrocierre
INTO vfecha, vtipoprepaga, vcodprepag,
vtipopada, vcodpadr, vidpad,
vtipoarta, vcodartr,  
vcodambsuc, vcodambtur, vnrocierre, vtipocom 
FROM comm INNER JOIN pad ON pad.id = comm.idpad            
WHERE NEW.idcomm = comm.id;
SELECT art.tipoarta, art.codartr
INTO vtipoarta, vcodartr
FROM art
WHERE art.id = NEW.idart;
/*pre_obtenerprecioctxt(character varying, integer, character varying, character varying, character varying, character varying, character varying, integer, character varying,
character varying, character varying, character varying, character varying, double precision, character varying, integer)  */
INSERT INTO artmpre 
(SELECT NEW.id, pre.id, pre_obtenerprecioctxt(pre.cod, NEW.idart,
vfecha, 
vtipoprepaga, vcodprepag,
vtipopada, vcodpadr, vidpad,
vtipoarta, vcodartr,
vfecha, vfecha,
vcodambsuc, 0.00,
vcodambtur, vnrocierre, vtipocom)                                                                   
FROM pre
WHERE pre.artmpre = 'S');
RETURN NEW;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION tg_artm_ins_artmpre()
  OWNER TO abacouser;
GRANT EXECUTE ON FUNCTION tg_artm_ins_artmpre() TO abacouser;
GRANT EXECUTE ON FUNCTION tg_artm_ins_artmpre() TO public;


--==================================
SELECT aaVersion('445.01','445.00');
--==================================
